<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>添加学生</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../../resources/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="../../../resources/layuiadmin/style/admin.css" media="all">
</head>
<body>

  <div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-header">添加学生信息</div>
      <div class="layui-card-body" style="padding: 15px;">
        <form class="layui-form" action="" lay-filter="add-studentInfo">

          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">学生头像</label>
              <input type="file" name="profileImg">
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label">学生号</label>
              <div class="layui-input-inline">
                <input type="text" name="studentId" lay-verify="required|studentId" autocomplete="off" class="layui-input">
              </div>
            </div>
            <div class="layui-inline">
              <label class="layui-form-label">学生姓名</label>
              <div class="layui-input-inline">
                <input type="text" name="studentName" lay-verify="required|studentName" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
              <input type="radio" name="gender" value="男" title="男">
              <input type="radio" name="gender" value="女" title="女">
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">邮箱</label>
            <div class="layui-input-block">
              <input type="text" name="email" lay-verify="email" placeholder="" autocomplete="off" class="layui-input">
            </div>
          </div>
          <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">简介</label>
            <div class="layui-input-block">
              <textarea name="studentDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label">所属班级</label>
            <div class="layui-input-block">
              <div class="layui-col-md6">
                <select id="clazz">
                </select>
              </div>
            </div>
          </div>

          <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
              <div class="layui-footer" style="left: 0;">
                <button class="layui-btn" lay-submit="" lay-filter="add-student">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
              </div>
            </div>
          </div>

        </form>
      </div>
    </div>
  </div>

    
  <script src="../../../resources/layuiadmin/layui/layui.js"></script>
  <script src="../../../resources/js/common.js"></script>
  <script>
  layui.config({
    base: '../../../resources/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'form'], function(){
    var $ = layui.$
    ,layer = layui.layer
    ,form = layui.form;

    form.render(null, 'add-studentInfo');

    /* 自定义验证规则 */
    form.verify({
      studentId: function(value){
        if(hasLetter(value) === true || isChina(value) === true){
          return '学生号不能含有中文';
        }
      }
      ,studentName: function(value){
        if(containsNumber(value) === true){
          return '学生姓名不能含有数字';
        }
      }
    });

    getClazzList();

    // 获取班级列表
    function getClazzList(){
      $.get('/admin/listClazzMap', function(res) {
        if (res.success){
          var tempHtml = '';
          //根据班级列表动态拼接html
          res.clazzList.map(function (item, index) {
            tempHtml += '<option data-id="' + item.clazzId + '">'
                    + item.clazzDesc + '</option>';
          });
          $('#clazz').html(tempHtml);
          form.render('select');
        }
      });
    }

    var student = {};
    
    /* 监听提交 */
    form.on('submit(add-student)', function(data){
      // 获取缩略图文件流
      var studentImg = $('input[name="profileImg"]')[0].files[0];

      student.studentId = $('input[name="studentId"]').val();
      student.studentName = $('input[name="studentName"]').val();
      student.gender = $('input[name="gender"]:checked').val();
      student.email = $('input[name="email"]').val();
      student.studentDesc = $('textarea[name="studentDesc"]').val();
      student.clazz = {
        clazzId : $('#clazz').find('option').not(function () {
          return !this.selected;
        }).data('id')
      };

      var formData = new FormData();
      formData.append('studentImg', studentImg);
      formData.append('studentStr',JSON.stringify(student));

      //用ajax时需要注意你的url接口、采用哪一种方式type获取，它的使用的哪种数据类型datatype
      $.ajax({
        url:'/admin/addStudent',
        type:'post',
        data:formData,
        contentType : false,
        processData : false,
        success:function(res){
          if (res.success){
            layer.msg("添加学生信息成功");
          }else {
            layer.msg("添加学生信息失败" + res.errMsg);
          }
        }
      })
      return false;
    });

  });
  </script>
</body>
</html>
